package com.zyl.pts.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyl.pts.config.enums.CodeAndMessageEnum;
import com.zyl.pts.domain.bean.IndexRecord;
import com.zyl.pts.domain.dto.IRDTO.IRConditionalDTO;
import com.zyl.pts.domain.dto.IRDTO.IRUpdateOrAddDTO;
import com.zyl.pts.mapper.AnimalMapper;
import com.zyl.pts.mapper.IRMapper;
import com.zyl.pts.service.IRService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: product_source
 * @Package: com.zyl.pts.service.serviceImpl
 * @ClassName: IRServiceImpl
 * @Description TODO()
 * @Author 翟玉龙
 * @Date 2024/11/19-19:11
 */
@Service
public class IRServiceImpl implements IRService {
    private final IRMapper irMapper;
    private final AnimalMapper animalMapper;

    public IRServiceImpl(IRMapper irMapper, AnimalMapper animalMapper) {
        this.irMapper = irMapper;
        this.animalMapper = animalMapper;
    }

    /**
     * 查询指定参数指标记录
     * @param irConditionalDTO 指标记录查询条件
     * @return {@link PageInfo }<{@link IndexRecord }>
     */
    @Override
    public PageInfo<IndexRecord> queryIndexRecordByPage(IRConditionalDTO irConditionalDTO) {
        Page<IndexRecord> page = PageHelper.startPage(irConditionalDTO.getPageNum(), irConditionalDTO.getPageSize());
        List<IndexRecord> indexRecords = irMapper.queryIndexes(irConditionalDTO.getStartWeight(), irConditionalDTO.getEndWeight());
        PageInfo<IndexRecord> pageInfo = new PageInfo<>(indexRecords);
        page.close();
        return pageInfo;
    }

    /**
     * 新增或者修改指标记录
     * @param indexRecord 指标记录
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum addOrUpdateIndexRecord(IRUpdateOrAddDTO indexRecord) {
        if(animalMapper.existsById(indexRecord.getIrdAnimalId()) == 0) {
            return CodeAndMessageEnum.ERROR_ANIMAL_NOT_EXIST;
        }
        /*
         * 获取id ，判断新增还是修改
         */
        Integer irdId = indexRecord.getIrdId();
        if (irdId == null) {
            //新增
            if(irMapper.addIndexRecord(indexRecord) == 1) {
                return updateAnimalSelective(indexRecord, CodeAndMessageEnum.INSERT_SUCCESS);
            }
            return CodeAndMessageEnum.ERROR_INSERT;
        } else {
            if(irMapper.updateIndexRecord(indexRecord) == 1) {
                return updateAnimalSelective(indexRecord, CodeAndMessageEnum.UPDATE_SUCCESS);
            }
            return CodeAndMessageEnum.ERROR_UPDATE;
        }
    }

    /**
     * 对指定参数进行修改
     */
    public CodeAndMessageEnum updateAnimalSelective(IRUpdateOrAddDTO indexRecord, CodeAndMessageEnum codeAndMessageEnum) {
        if(StringUtils.hasText(indexRecord.getIrdHealthy()) ||
                StringUtils.hasText(indexRecord.getIrdWeight())) {
            return animalMapper.updateAnimalSelective(indexRecord.getIrdWeight(), indexRecord.getIrdHealthy(), indexRecord.getIrdAnimalId()) == 1 ?
                    codeAndMessageEnum : CodeAndMessageEnum.ERROR;
        }
        return null;
    }

    /**
     * 根据id删除指标记录
     * @param irdId 指标记录id
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum deleteIndexRecordById(Integer irdId) {
        return irMapper.deleteIndexRecordById(irdId) == 1 ?
                CodeAndMessageEnum.DELETE_SUCCESS : CodeAndMessageEnum.ERROR_NOT_EXIST;
    }

    /**
     * 根据ids删除指标记录
     * @param irdIds 指标记录ids
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum deleteIndexRecordByIds(List<Integer> irdIds) {
        return irMapper.deleteIndexRecordByIds(irdIds) ?
                CodeAndMessageEnum.DELETE_MANY_SUCCESS : CodeAndMessageEnum.ERROR_NOT_EXIST;
    }
}
